package Json2RDF.Surface2RDF.SurfaceWriter;

import Json2RDF.Json2Java.OntologyClasses.ConicalSurface;
import Json2RDF.Surface2RDF.IndexTable.FaceIndexTable;
import Json2RDF.Surface2RDF.IndexTable.Face2ResourceIndexTable;
import RDFManager.PropertySet;
import RDFManager.RDFHead;
import RDFManager.RDFReader;
import RDFManager.ResourceSet;

import java.util.List;
import java.util.stream.IntStream;

import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;

/**
 * {@code ConicalSurfaceWriter}类提供将Conical_Surface写入RDF文件的方法.
 */
public class ConicalSurfaceWriter {

  /**
   * 遍历List中每个元素，将其对应数据属性值写入RDF文件，并添加到HashMap中.
   *
   * @param conicalSurfaceList 待写入的Conical_Surface列表
   */
  public static void writeList(List<ConicalSurface> conicalSurfaceList) {
    IntStream.range(0, conicalSurfaceList.size()).forEach(i -> {
      write(conicalSurfaceList.get(i), "conicalSurface" + (i + 1));
      Face2ResourceIndexTable.faceResourceIdxTbl.computeIfAbsent(conicalSurfaceList.get(i), v -> RDFReader.model.getResource(RDFHead.namespace + "conicalSurface" + (i + 1)));
      FaceIndexTable.faceIdxTbl.computeIfAbsent(conicalSurfaceList.get(i).getId(), v -> conicalSurfaceList.get(i));
    });
  }

  private static void write(ConicalSurface conicalSurface, String name) {
    Resource inst = RDFReader.model.createResource(RDFHead.namespace + name);
    RDFReader.model.add(inst, RDF.type, ResourceSet.Face);
    RDFReader.model.add(inst, RDF.type, ResourceSet.conicalSurface);
    inst.addLiteral(PropertySet.hasID, conicalSurface.getId());
    inst.addLiteral(PropertySet.hasTopRadius, conicalSurface.getTopRadius());
    inst.addLiteral(PropertySet.hasBottomRadius, conicalSurface.getBottomRadius());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasAxisEnd1", conicalSurface.getAxisEnd1());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasAxisEnd2", conicalSurface.getAxisEnd2());
    inst.addLiteral(PropertySet.hasHeight, conicalSurface.getHeight());
  }
}
